<template>
  <el-card class="attr-table">
    <el-form>
      <el-form-item label="属性名">
        <el-input
          placeholder="请输入属性名"
          class="attr-input"
          v-model="attrFormData.attrName"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          :icon="Plus"
          type="primary"
          @click="addAttr"
          :disabled="!attrFormData.attrName"
          >添加属性值</el-button
        >
        <el-table border :data="attrFormData.attrValueList" class="attr-table">
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="60"
          ></el-table-column>
          <el-table-column label="属性值名称">
            <template v-slot="{ row, $index }">
              <el-input
                v-if="row.isShow"
                v-model="row.valueName"
                @blur="addAttrValue(row, $index)"
                ref="inputRef"
              />
              <span v-else @click="showInput(row)" class="attr-span">{{
                row.valueName
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="60">
            <template v-slot="{ row, $index }">
              <el-popconfirm
                :title="`你确定要删除${row.valueName}吗`"
                @confirm="removeAttrValue($index)"
              >
                <template #reference>
                  <el-button type="danger" :icon="Delete"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="isShowDisabled"
          type="primary"
          @click="saveAttrValue"
          >确定</el-button
        >
        <el-button @click="goAttrList">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SaveUpdateAttr",
};
</script>

<script lang="ts" setup>
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { ref, reactive, nextTick, computed } from "vue";
import { saveOrUpdateAttrApi } from "@/api/product/attrList";
import { useCategoryStore } from "@/stores/category";
import { ElMessage } from "element-plus";
import type {
  SaveAttrItem,
  SaveAttrValueItem,
} from "@/api/product/model/attrListModel";
const props = defineProps<{
  attr: SaveAttrItem;
}>();
const inputRef = ref();
const CategoryStore = useCategoryStore();

const saveAttrValue = async () => {
  await saveOrUpdateAttrApi({
    ...attrFormData,
    categoryId: CategoryStore.category3Id as number,
    categoryLevel: 3,
  });
  ElMessage.success("添加属性值成功");

  goAttrList();
};
//实现返回AttrList组件
const isShowDisabled = computed(() => {
  return (
    !attrFormData.attrName ||
    !attrFormData.attrValueList.some((attrValue) => attrValue.valueName)
  );
});
const emit = defineEmits(["setIsShowAttrList"]);
const goAttrList = () => {
  emit("setIsShowAttrList", true);
};
//定义表单数据
const attrFormData = reactive<SaveAttrItem>(props.attr);
//增加属性值并聚焦
const addAttr = async () => {
  attrFormData.attrValueList.push({
    isShow: true,
    valueName: "",
  });
  await nextTick();
  inputRef.value.focus();
};
//展示span标签
const addAttrValue = (row: SaveAttrValueItem, index: number) => {
  //没有输入内容则删除该行
  if (!row.valueName) {
    attrFormData.attrValueList.splice(index, 1);
  }
  row.isShow = false;
};
//切换到编辑模式并聚焦
const showInput = async (row: SaveAttrValueItem) => {
  row.isShow = true;
  await nextTick();
  inputRef.value.focus();
};
const removeAttrValue = (index: number) => {
  attrFormData.attrValueList.splice(index, 1);
};
</script>

<style scoped>
.attr-table {
  margin-top: 10px;
}
.attr-input {
  width: 200px;
}
.attr-span {
  display: inline-block;
  width: 100%;
}
</style>
